package com.shuyuntu.proposal.service;

import com.shuyuntu.proposal.dao.IAttachmentDao;
import com.shuyuntu.proposal.entity.Attachment;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.crypto.ExemptionMechanismException;
import java.util.List;

/**
 * Created by limeng on 16-5-22.
 */
@Service
public class AttachmentService {

    @Transactional
    public Integer insertAttachment(Attachment attachment) {
        attachment.setPath("attachments");
        return attachmentDao.insert(attachment);
    }

    @Transactional
    public boolean update(Attachment attachment) {
        try {
            attachmentDao.update(attachment);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Transactional
    public List<Attachment> getListByProposalId(int id) {
        Criterion cr1 = Restrictions.eq("proposal.id", id);
        return attachmentDao.queryList(cr1);
    }

    @Transactional
    public Attachment getById(Integer attachmentId) {
        return attachmentDao.queryById(attachmentId);
    }

    @Resource
    IAttachmentDao attachmentDao;

    public IAttachmentDao getAttachmentDao() {
        return attachmentDao;
    }

    public void setAttachmentDao(IAttachmentDao attachmentDao) {
        this.attachmentDao = attachmentDao;
    }
}
